<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\cms\api\controller;

use think\exception\ValidateException;
use app\api\BaseController;
use plugins\cms\api\validate\News as Validate;
use plugins\cms\api\model\NewsCollect;
use app\api\model\User;
use plugins\cms\api\model\News as NewsModel;
/**
 * 招聘
 */
class News extends BaseController
{
    /**
     * 列表
     */
    public function index()
    {
        if ($this->request->isPost()) {
            try {
                $input = input('post.');
                validate(Validate::class)->scene('index')->check($input);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }
            $order = ! empty($input['prop']) && ! empty($input['order']) ? [$input['prop'] => $input['order']] : ['istop' => 'desc', 'sort' => 'desc', 'create_time' => 'desc'];
            $append = ['url','content_images','comment_count','collect_count','catalog_title', 'friend_time', 'is_collect'];
            $search = ['keyword', 'date', 'attr', 'catalog_id', 'status'];
            $data   = NewsModel::withSearch($search, $input)->order($order)->append($append)->page($input['page'],10)->select();
            $count  = NewsModel::withSearch($search, $input)->count();
            return json(['status' => 'success','message' => '获取成功', 'data' => $data, 'count' => $count]);
        }
    }

    /**
     * 详情
     */
    public function single()
    {
        if ($this->request->isPost()) {
            try {
                $input = input('post.');
                validate(Validate::class)->scene('single')->check($input);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }
            $where[] = ['id', '=', $input['id']];
            $append  = ['content_images','comment_count','collect_count','catalog_title', 'friend_time','relation_list', 'is_collect'];
            $data    = NewsModel::where($where)->append($append)->find();
            if (! $data) {
                return json(['status'=>'error', 'message'=> '新闻信息不存在']);
            }
            // 权限
            if (! empty($data->group_id)) {
                if (empty($this->request->userInfo)) {
                    return json(['status' => 'error','message' => '访问权限不足']);
                }
                if (! in_array($this->request->userInfo->group_id, $data->group_id)) {
                    return json(['status' => 'error','message' => '访问权限不足']);
                }
            }
            // 上下条
            if (! empty($input['catalog_id'])) {
                $field = 'id,catalog_id,title,seo_url';
                $npWhere[] = ['catalog_id', 'find in set', $input['catalog_id']];
                $npWhere[] = ['status', '=', 1];
                $data->prev = NewsModel::where('id','>',$data->id)->field($field)->where($npWhere)->order('id', 'asc')->find();
                $data->next = NewsModel::where('id','<',$data->id)->field($field)->where($npWhere)->order('id', 'desc')->find();
            }
            return json(['status' => 'success','message' => '获取成功', 'data' => $data]);
        }
    }
}